package com.starsea.book.ui.newpage.adapter

import android.content.Context
import android.os.Bundle
import android.view.ViewGroup
import com.starsea.book.base.adapter.ItemViewHolder
import com.starsea.book.base.adapter.RecyclerAdapter
import com.starsea.book.databinding.ItemMineFeedBackListBinding
import com.starsea.book.ui.newpage.entity.FeedBackListEntity
import com.starsea.book.ui.widget.recycler.ItemTouchCallback

class FeedBackListAdapter(
    context: Context,
    var onChanged: ((value: FeedBackListEntity.List) -> Unit)? = null
) :
    RecyclerAdapter<FeedBackListEntity.List, ItemMineFeedBackListBinding>(context),
    ItemTouchCallback.Callback {


    override fun getViewBinding(parent: ViewGroup): ItemMineFeedBackListBinding {
        return ItemMineFeedBackListBinding.inflate(inflater, parent, false)
    }

    override fun registerListener(holder: ItemViewHolder, binding: ItemMineFeedBackListBinding) {
        binding.root.setOnClickListener {
            getItem(holder.layoutPosition)?.let {
                onChanged!!.invoke(it)
            }

        }
    }

    override fun convert(
        holder: ItemViewHolder,
        binding: ItemMineFeedBackListBinding,
        item: FeedBackListEntity.List,
        payloads: MutableList<Any>
    ) {
        binding.run {
            val bundle = payloads.getOrNull(0) as? Bundle
            if (bundle == null) {
                tvTitle.text = item.content
                tvTime.text = item.createTime
                tvStaus.text = if (item.replyStatus == 0) "待回复" else "已回复"
            } else {
                bundle.keySet().map {
                    when (it) {
                        "content" -> tvTitle.text = item.content
                        "createTime" -> tvTime.text = item.createTime
                        "replyStatus" -> tvStaus.text =
                            if (item.replyStatus == 0) "待回复" else "已回复"
                    }
                }
            }
        }
    }
}